package cn.stylefeng.guns.cp;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.HttpUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import org.apache.commons.compress.utils.Lists;

import java.io.File;
import java.nio.charset.Charset;
import java.util.*;

/**
 * @Description TODO
 * @Author xiesm
 * @Date 2023/12/21 11:10
 **/
public class ScUtil {

    public static void main(String[] args) {
//        String path = "D:\\home\\code\\极速赛车\\result.xls";
//        List<Sc10> scList = getCodesFromExcel(path);
////        check(scList, 5, "08 07 04 01 10");
//        check(scList, 4, "03 02 06 10");

//        String path = "D:\\home\\code\\澳洲10\\today.txt";
//        String path2 = "D:\\home\\code\\澳洲10\\result.xls";
//        writeAo10ToExcel(path, path2, true);
        getResult();//获取全部结果
        writeAo10();//写入澳10
        filterAo10(200);//过滤ao10
        writeSc10();//写入赛车
        filterSc10(200);//过滤赛车
        writeFt10();//写入飞艇
        filterFt10(200);//过滤飞艇
        new Thread(() -> {
        }).start();
        new Thread(() -> {
        }).start();
        new Thread(() -> {
        }).start();

//        readSc10();
//        getSc10();
//        test();
    }

    private static String token = "96bfdcdfa2d64bc09645f57e7df84ef81703237339437";

    public static void getResult() {
        JSONObject ft = sendSc(token, "48", "1", 0);
        JSONObject sc = sendSc(token, "67", "2", 0);
        JSONObject ao = sendAo("2023-12-22", "10012");
        FileUtil.writeString(ft.toJSONString(), "D:\\home\\code\\极速飞艇\\近1000.txt", Charset.defaultCharset());
        FileUtil.writeString(sc.toJSONString(), "D:\\home\\code\\极速赛车\\近1000.txt", Charset.defaultCharset());
        FileUtil.writeString(ao.toJSONString(), "D:\\home\\code\\澳洲10\\today.txt", Charset.defaultCharset());
    }

    //请求赛车结果
    public static JSONObject sendSc(String token, String ticketId, String type, Integer num) {
        //https://cgcpapiyop2.egoeg4i.com:10151/coron/api/ticketSourceResult/ticketSourceResultList.json
        String url = "http://124.222.75.100:8089/cp/sc?token={}&ticketId={}&type={}&num={}";
        url = StrUtil.format(url, token, ticketId, type, num);
        String res = HttpUtil.createGet(url).execute().body();
        return JSONObject.parseObject(res);
    }

    //请求澳10结果
    public static JSONObject sendAo(String date, String lotCode) {
        String url = "http://124.222.75.100:8089/cp/ao?date={}&lotCode={}";
        url = StrUtil.format(url, date, lotCode);
        String res = HttpUtil.createGet(url).execute().body();
        return JSONObject.parseObject(res);
    }

    //过滤最近组数-赛车
    public static void filterSc10(int num) {
        List<String> codes = FileUtil.readLines("D:\\home\\code\\sc10-all.txt", Charset.defaultCharset());
        getSc10(codes, "D:\\home\\code\\极速赛车\\result-sc.xls", "D:\\home\\code\\极速赛车\\sc10-filter.txt", num);
    }

    //过滤最近组数-飞艇
    public static void filterFt10(int num) {
        List<String> codes = FileUtil.readLines("D:\\home\\code\\sc10-all.txt", Charset.defaultCharset());
        getSc10(codes, "D:\\home\\code\\极速飞艇\\result-ft.xls", "D:\\home\\code\\极速飞艇\\ft10-filter.txt", num);
    }

    //过滤最近组数-澳10
    public static void filterAo10(int num) {
        List<String> codes = FileUtil.readLines("D:\\home\\code\\sc10-all.txt", Charset.defaultCharset());
        getSc10(codes, "D:\\home\\code\\澳洲10\\result-ao10.xls", "D:\\home\\code\\澳洲10\\ao10-filter.txt", num);
    }

    public static void getSc10(List<String> codes, String sourcePath, String targetPath, int num) {
        List<Sc10> sc10List = getCodesFromExcel(sourcePath);
        for (int i = 1; i < num + 1; i++) {
            Sc10 sc10 = sc10List.get(i);
            String lxCode = sc10.getLxCode(4);
            codes.remove(lxCode);
        }
        FileUtil.writeLines(codes, targetPath, Charset.defaultCharset());
    }

    //写入最新数据-飞艇10
    public static void writeFt10() {
        String path = "D:\\home\\code\\极速飞艇\\近1000.txt";
        String path2 = "D:\\home\\code\\极速飞艇\\result-ft.xls";
        writeSc10ToExcel(path, path2, true);
    }

    //写入最新数据-赛车10
    public static void writeSc10() {
        String path = "D:\\home\\code\\极速赛车\\近1000.txt";
        String path2 = "D:\\home\\code\\极速赛车\\result-sc.xls";
        writeSc10ToExcel(path, path2, true);
    }

    //写入最新数据-澳10
    public static void writeAo10() {
        String path = "D:\\home\\code\\澳洲10\\today.txt";
        String path2 = "D:\\home\\code\\澳洲10\\result-ao10.xls";
        writeAo10ToExcel(path, path2, true);
    }

    //写入赛车数据到excel
    public static void writeSc10ToExcel(String sourcePath, String targetPath, Boolean boo) {
        String sourceStr = FileUtil.readString(sourcePath, Charset.defaultCharset());
        JSONObject sourceJSON = JSONObject.parseObject(sourceStr);

        List<Sc10> sourceList = Lists.newArrayList();
        JSONArray datas = sourceJSON.getJSONArray("data");
        List<List<String>> rows = Lists.newArrayList();
        for (int i = 0; i < datas.size(); i++) {
            List<Object> result = Lists.newArrayList();

            JSONObject data = datas.getJSONObject(i);
            String issue = data.getString("issue");
            result.add(issue);

            String[] codes = data.getString("code").split(" ");
            for (int j = 0; j < codes.length; j++) {
                String code = StrUtil.padPre(codes[j], 2, "0");
                result.add(code);
            }
            Sc10 sc10 = new Sc10(result);
            sourceList.add(sc10);
        }
        ExcelWriter writer = ExcelUtil.getWriter(new File(targetPath));
        if (boo) {
            List<Sc10> scs = getCodesFromExcel(targetPath);
            Sc10.addAll(sourceList, scs);
            Collections.sort(sourceList);
            writer.write(sourceList, false);
        } else {
            writer.write(sourceList);
        }
        writer.flush();
        writer.close();
    }

    //写入澳10数据到excel
    public static void writeAo10ToExcel(String sourcePath, String targetPath, Boolean boo) {
        String sourceStr = FileUtil.readString(sourcePath, Charset.defaultCharset());
        JSONObject sourceJSON = JSONObject.parseObject(sourceStr);
        JSONArray datas = sourceJSON.getJSONObject("result").getJSONArray("data");
        List<Sc10> lists = Lists.newArrayList();
        for (int i = 0; i < datas.size(); i++) {
            JSONObject data = datas.getJSONObject(i);
            Sc10 sc = new Sc10(data.getString("preDrawIssue"), Arrays.asList(data.getString("preDrawCode").split(",")));
            lists.add(sc);
        }
        ExcelWriter writer = ExcelUtil.getWriter(new File(targetPath));
        if (boo) {
            List<Sc10> scs = getCodesFromExcel(targetPath);
            Sc10.addAll(lists, scs);
            Collections.sort(lists);
            writer.write(lists, false);
        } else {
            writer.write(lists);
        }
        writer.flush();
        writer.close();
    }

    //从excel获取数据
    public static List<Sc10> getCodesFromExcel(String path) {
        ExcelReader reader = ExcelUtil.getReader(new File(path));
        List<List<Object>> rows = reader.read();

        List<Sc10> scList = Lists.newArrayList();
        for (int i = 0; i < rows.size(); i++) {
            List<Object> objects = rows.get(i);
            String[] codes = new String[10];
            for (int j = 1; j < objects.size(); j++) {
                codes[j - 1] = objects.get(j).toString();
            }
            Sc10 sc10 = new Sc10(objects);
            scList.add(sc10);
        }
        reader.close();
        return scList;
    }

    //比较code
    public static void check(List<Sc10> scList, int num, String codes) {
        for (int i = 0; i < scList.size(); i++) {
            Sc10 sc10 = scList.get(i);
            StringBuffer sb = new StringBuffer();
            for (int j = 0; j < num; j++) {
                sb.append(sc10.getCode(j + 1)).append(" ");
            }
            if (Objects.equals(codes, sb.toString().trim())) {
                System.out.println(sc10);
            }
        }
    }
}
